package com.butioy.study.jersey.handler;

import com.butioy.study.jersey.exception.NotLoginException;
import com.butioy.study.jersey.pojo.Result;
import javax.inject.Singleton;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yanglq
 * @since 3.0.0
 */
@Slf4j
@Provider
@Singleton
public class GlobalExceptionHandler implements ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable e) {
        log.error("系统发生异常, 异常信息:{}", e.getMessage(), e);

        String errMsg = "服务器内部错误";
        if (e instanceof NotLoginException) {
            errMsg = e.getMessage();
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Result.error(errMsg)).build();
    }
}
